package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.chat.event.manager.UnReadMessageManager;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomUsersResponse;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;
import io.rong.imlib.RongIMClient;
import utils.Utils;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

/**
 * 作者：lm
 * <p>
 * 创建日期：2018/9/14$
 * <p>
 * 描述：
 */
public class MyConversationListAdapter extends RecyclerView.Adapter<MyConversationListAdapter.MyConversationListHolder> {

    private Context context;
    private List<GetRoomUsersResponse.ListBean> listBeans;
    private  MyConversationLisListener changLongListener;
    public MyConversationListAdapter(Context context, List<GetRoomUsersResponse.ListBean> listBeans) {
        this.context = context;
        this.listBeans = listBeans;
    }
    public void setChangLongListener( MyConversationLisListener changLongListener){
        this.changLongListener=changLongListener;
    }
    @Override
    public MyConversationListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyConversationListHolder(LayoutInflater.from(context).inflate(R.layout.item_conversion_list, null));
    }

    @Override
    public void onBindViewHolder(final MyConversationListHolder holder, final int position) {
        if (listBeans.get(position)!=null){
            GetRoomUsersResponse.ListBean bean = listBeans.get(position);
            holder.tvName.setText(bean.getNickname());
            String result = "";
            String role = bean.getChatroom_role();
            if ("1".equals(role)) {
                //普通会员
                result = "会员";
            } else if ("2".equals(role)) {
                //超级管理元
                result = "超级管理员";
            } else if ("3".equals(role)) {
                //管理员
                result = "管理员";
            }
            holder.tvJiaose.setText(result);
            if (!Utils.isEmpty(bean.getAvatar())) {
                GlideHelper.loadImage(context, bean.getAvatar(), holder.ivIcon);
            } else {
                holder.ivIcon.setImageResource(R.mipmap.chat_default);
            }

            UnReadMessageManager.getInstance().getPrivateUnreadCount(new RongIMClient.ResultCallback<Integer>() {
                @Override
                public void onSuccess(Integer integer) {
                    if (integer > 0) {
                       holder. tvBubble.setVisibility(VISIBLE);
                        holder.tvBubble.setText(integer + "");
                    } else {
                        holder.tvBubble.setVisibility(GONE);
                        holder.tvBubble.setText(0 + "");
                    }
                }

                @Override
                public void onError(RongIMClient.ErrorCode errorCode) {

                }
            }, bean.getRy_id());

            int dataType = bean.getDataType();
            switch (dataType) {
                case 0:
                    holder.ivOnline.setVisibility(VISIBLE);
                    if (bean.isOnline()) {
                        //在线
                        holder .ivPreIcon.setVisibility(GONE);
                        holder. ivOnline.setSelected(true);
                    } else {
                        holder.ivPreIcon.setVisibility(VISIBLE);
                        holder.ivOnline.setSelected(false);
                    }
                    holder.tvPriviteChat.setVisibility(VISIBLE);
                    holder. ivDelete.setVisibility(GONE);
                    break;
                case 1:
                    holder.ivOnline.setVisibility(VISIBLE);
                    if (bean.isOnline()) {
                        //在线
                        holder.ivPreIcon.setVisibility(GONE);
                        holder.ivOnline.setSelected(true);
                    } else {
                        holder.ivPreIcon.setVisibility(VISIBLE);
                        holder.ivOnline.setSelected(false);
                    }
                    holder.ivPreIcon.setVisibility(GONE);
                    holder. tvPriviteChat.setVisibility(VISIBLE);
                    holder.ivDelete.setVisibility(GONE);
                    break;
                case 2:
                    holder. ivOnline.setVisibility(VISIBLE);
                    if (bean.isOnline()) {
                        //在线
                        holder.ivPreIcon.setVisibility(GONE);
                        holder.ivOnline.setSelected(true);
                    } else {
                        holder. ivPreIcon.setVisibility(VISIBLE);
                        holder.ivOnline.setSelected(false);
                    }
                    holder.ivPreIcon.setVisibility(GONE);
                    holder.tvPriviteChat.setVisibility(GONE);
                    holder. ivDelete.setVisibility(VISIBLE);
                    break;
            }

holder.llConversationItem.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        changLongListener.ChangLongClick(position,1);
    }
});
holder.tvPriviteChat.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        changLongListener.ChangLongClick(position,2);
    }
});
        }

    }

    @Override
    public int getItemCount() {
        return listBeans.size();
    }

    public class MyConversationListHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.ll_conversation_item)
        LinearLayout llConversationItem;
        @BindView(R.id.iv_icon)
        CircleImageView ivIcon;
        @BindView(R.id.iv_pre_icon)
        ImageView ivPreIcon;
        @BindView(R.id.tv_bubble)
        TextView tvBubble;
        @BindView(R.id.iv_online)
        ImageView ivOnline;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_jiaose)
        TextView tvJiaose;
        @BindView(R.id.tv_privite_chat)
        TextView tvPriviteChat;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        public MyConversationListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface  MyConversationLisListener{
        void ChangLongClick(int posion,int tag);
    }
}
